﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

namespace LitTools
{
    /// <summary>
    /// 窗口过滤器
    /// </summary>
    public class WindowFilter : FilterBase
    {
        public enum WindowType
        {
            Up,
            Down,
            Both
        }

        [Header("窗口类型")]
        public WindowType windowType;
        [Header("窗口列表")]
        public List<GameObject> windows;

        public override List<RaycastResult> FiltResults(List<RaycastResult> result)
        {
            if (windows == null
                || windows.Count == 0)
                return null;
            if (result == null)
                return result;
            switch (windowType)
            {
                case WindowType.Up:
                    return FiltResultUpWindow(result);
                case WindowType.Down:
                    return FiltResultDownWidnow(result);
                case WindowType.Both:
                    return FiltBothWindow(result);
                default:
                    break;
            }
            return result;
        }

        /// <summary>
        /// 保留窗口上的结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        private List<RaycastResult> FiltResultUpWindow(List<RaycastResult> result)
        {
            bool upWindow = true;
            for (int i = 0; i < result.Count; i++)
            {
                if (windows.Contains(result[i].gameObject))
                {
                    //射线照射到窗口，标记upWindow为false
                    //移除窗口，窗口不响应事件
                    upWindow = false;
                    result.RemoveAt(i);
                    i--;
                }
                if (!upWindow)
                {
                    result.RemoveAt(i);
                    i--;
                }
            }
            return result;
        }

        /// <summary>
        /// 保留窗口下的结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        private List<RaycastResult> FiltResultDownWidnow(List<RaycastResult> result)
        {
            bool downWindow = false;
            for (int i = 0; i < result.Count; i++)
            {
                if (windows.Contains(result[i].gameObject))
                {
                    //射线照射到窗口，标记downWindow为true
                    //移除窗口，窗口不响应事件
                    downWindow = true;
                    result.RemoveAt(i);
                    i--;
                }
                if (!downWindow)
                {
                    result.RemoveAt(i);
                    i--;
                }
            }
            return result;
        }

        /// <summary>
        /// 保留窗口两侧的结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        private List<RaycastResult> FiltBothWindow(List<RaycastResult> result)
        {
            bool containsWindow = false;
            for (int i = 0; i < result.Count; i++)
            {
                if (windows.Contains(result[i].gameObject))
                {
                    containsWindow = true;
                    result.RemoveAt(i);
                    i--;
                }
            }
            if (containsWindow)
                return result;
            else
                return null;
        }
    }
}
